<?php
/*
DROP TABLE IF EXISTS `stat`;
CREATE TABLE `stat` (
  `id` bigint(20) NOT NULL auto_increment,
  `ip` char(15) NOT NULL,
  `dateip` char(20) NOT NULL,
  `y` smallint(6) NOT NULL,
  `m` tinyint(4) NOT NULL,
  `d` tinyint(4) NOT NULL,
  `into` int(11) NOT NULL,
  `out` int(11) NOT NULL,
  `urs` char(20) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `dateip` (`dateip`),
  KEY `d` (`d`),
  KEY `y` (`y`,`m`,`d`),
  KEY `m` (`m`,`d`),
  KEY `ip` (`ip`)
)

 */
class stat
{
	static protected $table 	 = 'stat';	
	static protected $interval =300; //s
	static protected $online 	 =3600; //s
	static protected $key 	 ='stat_key'; //s
	
	static protected $total;
	
	static public function begin($urs=false)
	{
		global $db;
		$time 			= time();
		$cookie_time 	= (int)$_COOKIE[self::$key];
		//$cookie_id 		= (int)$_COOKIE[self::$key.'_id'];
		$ip				= g::ip();
		if($cookie_time<$time-self::$interval)
		{
		   $y = date('Y',$time);
		   $m = date('n',$time);
		   $d = date('j',$time);
		   $dateip = $y.$m.$d.str_replace('.','',$ip);
		   $sql = array(
		   		'ip'=>$ip,
				'dateip'=>$dateip,
				'y'=>$y,
				'm'=>$m,
				'd'=>$d,
				'into'=>$time,
				'out'=>$time
			);
		   $ext = "ON DUPLICATE KEY UPDATE `out`=$time ";
		   if($urs)$sql['urs'] = $urs;
		   self::$total=$db->insert(self::$table,$sql,$ext);
		   if(self::$total)
		   {
		   		setcookie(self::$key, $time, time()+3600*5);
		   }
		   return self::$total;
		}
		return 0;
	}
	static public function get($array='')
	{
	    $rs=array();
	    //榡ưѼ
	    $rskey = array('total','online','today','yesterday','month','last_month');
	    if(empty($array))
	    {
	        $array = $rskey;
	    }elseif(is_array($array))
	    {
	        $array=explode(',',$array);
	    }
	    //p
	    self::begin();
	    //
	    foreach($array as &$v)
	    {
	        $f		= '_'.$v;
	        $rs[$v] = self::$f();
	    }
	    return $rs;
	}
	protected function _total()
	{
	    global $db,$scfg;
		$rs=$db->rsArray("SHOW TABLE STATUS LIKE ? ",self::$table);
	    //$rs=$db->rsArray("SHOW TABLE STATUS FROM ".$scfg['db']['database']." WHERE Name = ? ",self::$table);
	    return $rs['Auto_increment'];
	}
	protected function _online()
	{
	    global $db;
	    $rs=$db->rsArray("select count(id) as cc from ".self::$table." where `out` > ? ",time()-self::$online);
	    return $rs['cc'];
	}
	protected function _today()
	{
	    global $db;
	    $rs=$db->rsArray("select count(id) as cc from ".self::$table." where y=:y and m=:m and d=:d ",array('y'=>date('Y'),'m'=>date('n'),'d'=>date('j')));
	    return $rs['cc'];
	}
	protected function _yesterday()
	{
	    global $db;
	    $rs=$db->rsArray("select count(id) as cc from ".self::$table." where y=:y and m=:m and d=:d ",array('y'=>date('Y'),'m'=>date('n'),'d'=>date('j')-1));
	    return $rs['cc'];
	}
	protected function _month()
	{
	    global $db;
	    $rs=$db->rsArray("select count(id) as cc from ".self::$table." where y=:y and m=:m ",array('y'=>date('Y'),'m'=>date('n')));
	    return $rs['cc'];
	}
	protected function _last_month()
	{
	    global $db;
	    $rs=$db->rsArray("select count(id) as cc from ".self::$table." where y=:y and m=:m ",array('y'=>date('Y'),'m'=>date('n')-1));
	    return $rs['cc'];
	}
}
?>